<?php

/**
 * 自定义函数文件
 * Created by PhpStorm.
 * User: tyll
 * Date: 2018/9/17
 * Time: 14:54
 */
/* * *****************************************************控制台辅助函数************************************************** */

/**
 * 处理树形结构
 * @param $list
 * @param $parentid
 * @return array
 */
function deal_tree($list, $parentid) {
    foreach ($list as $k => $v) {
        $v['selected'] = $v['id'] == $parentid ? 'selected' : '';
        $arr[] = $v;
    }
    $tree_arr = [];
    $str = "\$id:\$spacer\$name@";

    $tree = new \app\common\controller\Tree();
    $tree->init($arr);
    $select_str = $tree->get_tree(0, $str);
    $select_arr = explode('@', $select_str);
    $select_arr = array_filter($select_arr);
    foreach ($select_arr as $k => $v) {
        $temp_arr = explode(':', $v);
        $temp_arr[0] = (int) $temp_arr[0];
        $temp_arr[1] = str_replace('&nbsp;', ' ', $temp_arr[1]);
        $tree_arr[] = $temp_arr;
    }
    return $tree_arr;
}

/**
 * 调用接口，验证签名
 * @param $gen_time
 * @param $sign
 * @return int
 */
function check_api_sign($gen_time, $sign) {
    $my_time = time();
    if ($my_time - $gen_time > 30) {
        return 0;
    }
    $my_sign = md5($gen_time . $gen_time);
    if ($my_sign == $sign) {
        return 1;
    }
    return 0;
}

/**
 * 获取当天的开始时间和结束时间
 * @return array
 */
function get_today_time() {
    return [
        'start_time' => date('Y-m-d 00:00:00', time()),
        'end_time' => date('Y-m-d 23:59:59', time())
    ];
}

/**
 * 获取指定日期所在天的开始时间和结束时间
 * @return array
 */
function get_the_today_time($date) {
    return [
        'start_time' => date('Y-m-d 00:00:00', strtotime($date)),
        'end_time' => date('Y-m-d 23:59:59', strtotime($date))
    ];
}

/**
 * 获取当月的开始时间和结束时间
 * @return array
 */
function get_month_time(){
    return [
        'start_time' => date('Y-m-d H:i:s',mktime(0,0,0,date('m'),1,date('Y'))),
        'end_time' => date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('t'),date('Y')))
    ];
}

/**
 * 获取指定日期所在月的开始时间和结束时间
 * @param $date
 * @return array
 */
function get_the_month_time($date){
    return [
        'start_time' => date('Y-m-01 00:00:00', strtotime($date)),
        'end_time' => date('Y-m-d 23:59:59', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day'))
    ];
}

/**
 * 数字乘100
 * @param $number
 * @return float|int
 */
function deal_number_mul_100($number) {
    $number = $number * 100;
    return $number;
}

/**
 * 数字除100
 * @param $number
 * @return float|int
 */
function deal_number_div_100($number) {
    $number = $number / 100;
    return $number;
}

/**
 * 处理视频url
 * @param $video
 * @return mixed
 */
function deal_video_url($video){
    $data = [];
    $data['video_cover_pic'] = $video . '?x-oss-process=video/snapshot,t_0,f_jpg,w_120,m_fast';
    //$data['video_cover_pic'] = 'https://mdxundian.oss-cn-zhangjiakou.aliyuncs.com/static/logo.png';
    return $data;
}

/**
 * 获取两个时间相差天数
 * @param $start_time
 * @param $end_time
 * @return float
 */
function get_time_diff_day($start_time, $end_time) {
    $start_time = date('Y-m-d', strtotime($start_time));
    $end_time = date('Y-m-d', strtotime($end_time));
    $day = floor((strtotime($start_time) - strtotime($end_time)) / 86400);
    return $day;
}

/**
 * 计算两个日期相隔多少年，多少月，多少天
 * @param $date1
 * @param $date2
 * @return array
 */
function get_time_diff_date($date1,$date2){
    if(strtotime($date1) > strtotime($date2)){
        $tmp = $date2;
        $date2 = $date1;
        $date1 = $tmp;
    }
    list($Y1,$m1,$d1) = explode('-',$date1);
    list($Y2,$m2,$d2) = explode('-',$date2);
    $Y = $Y2 - $Y1;
    $m = $m2 - $m1;
    $d = $d2 - $d1;
    if($d < 0){
        $d += (int)date('t',strtotime("-1 month $date2"));
        $m--;
    }
    if($m < 0){
        $m += 12;
        $Y--;
    }

    $data['year'] = $Y;
    $data['month'] = $m;
    $data['day'] = $d;
    return $data;
}

/**
 * 获取文件大小
 * @param $size
 * @return string
 */
function get_flie_size($size){
    $dw = '';#pow(2,40)是2的40次方。
    if($size >= pow(2,20)){
        $dw = round($size / 1048576 * 100) / 100 . ' MB';
    }elseif($size >= pow(2,10)){
        $dw = round($size / 1024 * 100) / 100 . ' KB';
    }elseif($size >= pow(1,10)){
        $dw = 'B';
    }
    return $dw;
 }
